package com.jcrj.service;

import com.jcrj.dao.PoliceTeamDao;
import com.jcrj.entity.OPoliceReport;
import com.jcrj.entity.OPoliceTeam;
import com.jcrj.entity.OPoliceTeamDraft;
import com.jcrj.entity.OTeamHistory;
import com.jcrj.entity.OTeamUser;
import com.jcrj.entity.OUser;
import java.util.Date;
import java.util.List;

public class PoliceTeamService
{
  private PoliceTeamDao policeTeamDao;
  private MessageService messageService;

  public MessageService getMessageService()
  {
    return this.messageService;
  }
  public void setMessageService(MessageService messageService) {
    this.messageService = messageService;
  }
  public PoliceTeamDao getPoliceTeamDao() {
    return this.policeTeamDao;
  }
  public void setPoliceTeamDao(PoliceTeamDao policeTeamDao) {
    this.policeTeamDao = policeTeamDao;
  }

  public void savePoliceTeamDraft(OPoliceTeamDraft teamDraft)
  {
    this.policeTeamDao.saveTeamObj(teamDraft);
  }

  public void updatePoliceTeamDraft(OPoliceTeamDraft teamDraft) {
    this.policeTeamDao.updateTeamObj(teamDraft);
  }

  public void updatePoiceTeam(OPoliceTeam policeTeam, String users, String names) {
    String hql = "delete from OTeamUser o where o.policeId=" + policeTeam.getId();
    this.policeTeamDao.executeHql(hql);
    this.policeTeamDao.updatePoliceTeam(policeTeam);
    if ((users != null) && (!"".equals(users))) {
      String[] userArr = users.split(",");
      String[] nameArr = names.split(",");
      for (int i = 0; i < userArr.length; i++) {
        OTeamUser team = new OTeamUser();
        team.setActorId(Integer.valueOf(Integer.parseInt(userArr[i])));
        team.setActorName(nameArr[i]);
        team.setPoliceTeam(policeTeam);
        this.policeTeamDao.saveTeamUser(team);
      }
    }
  }

  private void sendMessage(String users) {
    if ((users != null) && (!"".equals(users))) {
      String[] userArr = users.split(",");
      for (int i = 0; i < userArr.length; i++) {
        OUser user = (OUser)this.policeTeamDao.getObject(new OUser(), Integer.parseInt(userArr[i]));
        if ((user.getPhone() != null) && (!"".equals(user.getPhone())))
          this.messageService.send(user.getPhone(), "您有新的警种协作,请尽快处理!");
      }
    }
  }

  public void saveSubmitNext(OPoliceTeam policeTeam, String users, String names, String status)
  {
    this.policeTeamDao.savePoliceTeam(policeTeam);
    if ((users != null) && (!"".equals(users))) {
      String[] userArr = users.split(",");
      String[] nameArr = names.split(",");
      for (int i = 0; i < userArr.length; i++) {
        OTeamUser team = new OTeamUser();
        team.setActorId(Integer.valueOf(Integer.parseInt(userArr[i])));
        team.setActorName(nameArr[i]);
        team.setPoliceTeam(policeTeam);
        team.setState("coming");
        team.setLevels(Integer.valueOf(2));
        this.policeTeamDao.saveTeamUser(team);
      }
    }
    OTeamUser team = new OTeamUser();
    team.setActorId(policeTeam.getUserId());
    team.setActorName(policeTeam.getUserName());
    team.setPoliceTeam(policeTeam);
    team.setLevels(Integer.valueOf(1));
    team.setState("coming");
    this.policeTeamDao.saveTeamUser(team);
    OTeamHistory history = new OTeamHistory();
    history.setUserId(policeTeam.getUserId());
    history.setUserName(policeTeam.getUserName());
    history.setCreateTime(new Date());
    history.setLevels(Integer.valueOf(1));
    history.setStatus("交下一步");
    history.setActorName(names);
    history.setPoliceId(policeTeam.getId());
    this.policeTeamDao.saveTeamHistory(history);
    if ("1".equals(status))
      sendMessage(users);
  }

  public void updateSubmitNext(OPoliceTeam policeTeam, String users, String names, String status) {
    this.policeTeamDao.updatePoliceTeam(policeTeam);
    if ((users != null) && (!"".equals(users))) {
      String[] userArr = users.split(",");
      String[] nameArr = names.split(",");
      for (int i = 0; i < userArr.length; i++) {
        OTeamUser team = new OTeamUser();
        team.setActorId(Integer.valueOf(Integer.parseInt(userArr[i])));
        team.setActorName(nameArr[i]);
        team.setPoliceTeam(policeTeam);
        team.setLevels(Integer.valueOf(2));
        this.policeTeamDao.saveTeamUser(team);
      }
    }
    OTeamUser team = new OTeamUser();
    team.setActorId(policeTeam.getUserId());
    team.setActorName(policeTeam.getUserName());
    team.setPoliceTeam(policeTeam);
    team.setState("coming");
    team.setLevels(Integer.valueOf(1));
    this.policeTeamDao.saveTeamUser(team);
    OTeamHistory history = new OTeamHistory();
    history.setUserId(policeTeam.getUserId());
    history.setUserName(policeTeam.getUserName());
    history.setCreateTime(new Date());
    history.setLevels(Integer.valueOf(1));
    history.setStatus("交下一步");
    history.setActorName(names);
    history.setPoliceId(policeTeam.getId());
    this.policeTeamDao.saveTeamHistory(history);
    if ("1".equals(status))
      sendMessage(users);
  }

  public void saveNextSubmit(OPoliceTeam policeTeam, String users, String names, OUser user, String content, int teamId, String status) {
    this.policeTeamDao.updatePoliceTeam(policeTeam);
    OTeamUser teamU = getTeamUser(teamId);
    if ((users != null) && (!"".equals(users))) {
      String[] userArr = users.split(",");
      String[] nameArr = names.split(",");
      for (int i = 0; i < userArr.length; i++) {
        if (Integer.parseInt(userArr[i]) == policeTeam.getUserId().intValue()) {
          String hql = "update OTeamUser o set o.levels=" + (teamU.getLevels().intValue() + 1) + " where o.policeTeam.id=" + policeTeam.getId() + " and o.actorId=" + policeTeam.getUserId();
          this.policeTeamDao.executeHql(hql);
        } else {
          OTeamUser team = new OTeamUser();
          team.setActorId(Integer.valueOf(Integer.parseInt(userArr[i])));
          team.setActorName(nameArr[i]);
          team.setState("coming");
          team.setPoliceTeam(policeTeam);
          team.setLevels(Integer.valueOf(teamU.getLevels().intValue() + 1));
          this.policeTeamDao.saveTeamUser(team);
        }
      }
    }
    OTeamHistory history = new OTeamHistory();
    history.setUserId(user.getId());
    history.setUserName(user.getName());
    history.setCreateTime(new Date());
    history.setLevels(teamU.getLevels());
    history.setStatus("交下一步");
    history.setActorName(names);
    history.setPoliceId(policeTeam.getId());
    this.policeTeamDao.saveTeamHistory(history);
    OPoliceReport report = new OPoliceReport();
    report.setContent(content);
    report.setReportTime(new Date());
    report.setUserName(user.getName());
    report.setPoliceId(policeTeam.getId());
    this.policeTeamDao.savePoliceReport(report);
    this.policeTeamDao.deleteTeamObj(teamU);
    if ("1".equals(status))
      sendMessage(users);
  }

  public void saveDaftNextSubmit(OPoliceTeam policeTeam, String users, String names, OUser user, String content, int teamId, String status)
  {
    this.policeTeamDao.updatePoliceTeam(policeTeam);
    OTeamUser teamU = getTeamUser(teamId);
    if ((users != null) && (!"".equals(users))) {
      String[] userArr = users.split(",");
      String[] nameArr = names.split(",");
      for (int i = 0; i < userArr.length; i++) {
        OTeamUser team = new OTeamUser();
        team.setActorId(Integer.valueOf(Integer.parseInt(userArr[i])));
        team.setActorName(nameArr[i]);
        team.setState("coming");
        team.setPoliceTeam(policeTeam);
        team.setLevels(Integer.valueOf(teamU.getLevels().intValue() + 1));
        this.policeTeamDao.saveTeamUser(team);
      }
    }
    OTeamHistory history = new OTeamHistory();
    history.setUserId(user.getId());
    history.setUserName(user.getName());
    history.setCreateTime(new Date());
    history.setLevels(teamU.getLevels());
    history.setStatus("交下一步");
    history.setActorName(names);
    history.setPoliceId(policeTeam.getId());
    this.policeTeamDao.saveTeamHistory(history);
    OPoliceReport report = new OPoliceReport();
    report.setContent(content);
    report.setReportTime(new Date());
    report.setUserName(user.getName());
    report.setPoliceId(policeTeam.getId());
    this.policeTeamDao.savePoliceReport(report);
    teamU.setLevels(Integer.valueOf(1));
    this.policeTeamDao.updateTeamObj(teamU);
    if ("1".equals(status))
      sendMessage(users);
  }

  public void updateCompleteTeam(OPoliceTeam team, OUser user, String content, int teamId)
  {
    this.policeTeamDao.updatePoliceTeam(team);
    OTeamUser teamU = getTeamUser(teamId);
    OTeamHistory history = new OTeamHistory();
    history.setUserId(user.getId());
    history.setUserName(user.getName());
    history.setCreateTime(new Date());
    history.setLevels(teamU.getLevels());
    history.setStatus("结束");
    history.setPoliceId(team.getId());
    this.policeTeamDao.saveTeamHistory(history);
    if ((content != null) && (!"".equals(content))) {
      OPoliceReport report = new OPoliceReport();
      report.setContent(content);
      report.setReportTime(new Date());
      report.setUserName(user.getName());
      report.setPoliceId(team.getId());
      this.policeTeamDao.savePoliceReport(report);
    }
    this.policeTeamDao.deleteTeamObj(teamU);
  }

  public void updateAllCompleteTeam(int teamId, OUser user) {
    OTeamUser team = getTeamUser(teamId);
    String hql = "delete OTeamUser o where o.policeTeam.id=" + team.getPoliceTeam().getId() + " and o.actorId!=" + user.getId();
    this.policeTeamDao.executeHql(hql);
    OPoliceTeam police = getPoliceTeamObj(team.getPoliceTeam().getId().intValue());
    police.setDel("1");
    this.policeTeamDao.updatePoliceTeam(police);
    team.setState("end");
    this.policeTeamDao.updateTeamObj(team);
    OTeamHistory history = new OTeamHistory();
    history.setUserId(user.getId());
    history.setUserName(user.getName());
    history.setCreateTime(new Date());
    history.setStatus("结束");
    history.setPoliceId(team.getPoliceTeam().getId());
    this.policeTeamDao.saveTeamHistory(history);
  }
  public void saveReport(OPoliceReport report) {
    this.policeTeamDao.savePoliceReport(report);
  }

  public OPoliceTeam getPoliceTeamObj(int id) {
    return (OPoliceTeam)this.policeTeamDao.getObject(new OPoliceTeam(), id);
  }

  public OPoliceTeamDraft policeTeamDraft(int id) {
    return (OPoliceTeamDraft)this.policeTeamDao.getObject(new OPoliceTeamDraft(), id);
  }
  public List<OPoliceTeamDraft> draftPoliceTeamList(String title, String startDate, String endDate, int currentPage, OUser user) {
    String hql = "from OPoliceTeamDraft o where  o.userId=" + user.getId();
    if ((title != null) && (!"".equals(title.trim()))) {
      hql = hql + " and o.title like '%" + title.trim() + "%'";
    }
    if ((startDate != null) && (!"".equals(startDate.trim()))) {
      hql = hql + " and o.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
    }
    if ((endDate != null) && (!"".equals(endDate.trim()))) {
      hql = hql + " and o.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
    }
    hql = hql + " order by o.id desc";
    return this.policeTeamDao.findByPage(hql, currentPage);
  }

  public long draftPoliceTeamCount(String title, String startDate, String endDate, int currentPage, OUser user) {
    String hql = "select count(id) from OPoliceTeamDraft o where  o.userId=" + user.getId();
    if ((title != null) && (!"".equals(title.trim()))) {
      hql = hql + " and o.title like '%" + title.trim() + "%'";
    }
    if ((startDate != null) && (!"".equals(startDate.trim()))) {
      hql = hql + " and o.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
    }
    if ((endDate != null) && (!"".equals(endDate.trim()))) {
      hql = hql + " and o.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
    }
    return this.policeTeamDao.countTotalByHql(hql).longValue();
  }

  public List<OPoliceTeam> inPoliceTeamList(String title, String startDate, String endDate, int currentPage, OUser user) {
    String hql = "from OPoliceTeam o where o.del='0' and  o.userId=" + user.getId();
    if ((title != null) && (!"".equals(title.trim()))) {
      hql = hql + " and o.title like '%" + title.trim() + "%'";
    }
    if ((startDate != null) && (!"".equals(startDate.trim()))) {
      hql = hql + " and o.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
    }
    if ((endDate != null) && (!"".equals(endDate.trim()))) {
      hql = hql + " and o.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
    }
    hql = hql + " order by o.id desc";
    return this.policeTeamDao.findByPage(hql, currentPage);
  }

  public long inPoliceTeamCount(String title, String startDate, String endDate, int currentPage, OUser user) {
    String hql = "select count(id) from OPoliceTeam o where o.del='0' and  o.userId=" + user.getId();
    if ((title != null) && (!"".equals(title.trim()))) {
      hql = hql + " and o.title like '%" + title.trim() + "%'";
    }
    if ((startDate != null) && (!"".equals(startDate.trim()))) {
      hql = hql + " and o.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
    }
    if ((endDate != null) && (!"".equals(endDate.trim()))) {
      hql = hql + " and o.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
    }
    return this.policeTeamDao.countTotalByHql(hql).longValue();
  }

  public List<OTeamUser> taskPoliceTeamList(String title, String startDate, String endDate, int currentPage, String users)
  {
    String hql = "from OTeamUser o where o.state='coming' and  o.actorId in(" + users + ")";
    if ((title != null) && (!"".equals(title.trim()))) {
      hql = hql + " and o.policeTeam.title like '%" + title.trim() + "%'";
    }
    if ((startDate != null) && (!"".equals(startDate.trim()))) {
      hql = hql + " and o.policeTeam.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
    }
    if ((endDate != null) && (!"".equals(endDate.trim()))) {
      hql = hql + " and o.policeTeam.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
    }
    hql = hql + " order by o.policeTeam.createTime desc";
    return this.policeTeamDao.findByPage(hql, currentPage);
  }

  public long taskPoliceTeamCount(String title, String startDate, String endDate, String users) {
    String hql = "select count(id) from OTeamUser o where o.state='coming' and  o.actorId in(" + users + ")";
    if ((title != null) && (!"".equals(title.trim()))) {
      hql = hql + " and o.policeTeam.title like '%" + title.trim() + "%'";
    }
    if ((startDate != null) && (!"".equals(startDate.trim()))) {
      hql = hql + " and o.policeTeam.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
    }
    if ((endDate != null) && (!"".equals(endDate.trim()))) {
      hql = hql + " and o.policeTeam.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
    }
    return this.policeTeamDao.countTotalByHql(hql).longValue();
  }

  public List<OTeamUser> completePoliceTeamList(String title, String startDate, String endDate, int currentPage, OUser user)
  {
    String hql = "from OTeamUser o where o.state='end' and  o.actorId in(" + user.getId() + ")";
    if ((title != null) && (!"".equals(title.trim()))) {
      hql = hql + " and o.policeTeam.title like '%" + title.trim() + "%'";
    }
    if ((startDate != null) && (!"".equals(startDate.trim()))) {
      hql = hql + " and o.policeTeam.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
    }
    if ((endDate != null) && (!"".equals(endDate.trim()))) {
      hql = hql + " and o.policeTeam.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
    }
    hql = hql + " order by o.policeTeam.createTime desc";
    return this.policeTeamDao.findByPage(hql, currentPage);
  }

  public long completePoliceTeamCount(String title, String startDate, String endDate, int currentPage, OUser user) {
    String hql = "select count(id) from OTeamUser o where o.state='end' and  o.actorId in(" + user.getId() + ")";
    if ((title != null) && (!"".equals(title.trim()))) {
      hql = hql + " and o.policeTeam.title like '%" + title.trim() + "%'";
    }
    if ((startDate != null) && (!"".equals(startDate.trim()))) {
      hql = hql + " and o.policeTeam.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
    }
    if ((endDate != null) && (!"".equals(endDate.trim()))) {
      hql = hql + " and o.policeTeam.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
    }
    return this.policeTeamDao.countTotalByHql(hql).longValue();
  }

  public List<OTeamUser> findTeamUserList(int policeId) {
    String hql = "from OTeamUser o where o.policeId=" + policeId;
    return this.policeTeamDao.findByHql(hql);
  }

  public List<OTeamHistory> findTeamHistoryList(int policeId)
  {
    String hql = "from OTeamHistory o where o.policeId=" + policeId + " order by o.levels asc,o.id asc";
    return this.policeTeamDao.findByHql(hql);
  }

  public List<OPoliceReport> findReportList(int policeId) {
    String hql = "from OPoliceReport o where o.policeId=" + policeId + " order by o.id asc";
    return this.policeTeamDao.findByHql(hql);
  }

  public void deletePoliceTeamDraft(String[] ids) {
    if ((ids != null) && (ids.length > 0)) {
      String policeId = "";
      for (int i = 0; i < ids.length; i++) {
        if ("".equals(policeId))
          policeId = policeId + ids[i];
        else {
          policeId = policeId + "," + ids[i];
        }
      }
      String hql = "delete from OPoliceTeamDraft o where o.id in(" + policeId + ")";
      this.policeTeamDao.executeHql(hql);
    }
  }

  public void deletePoliceTeam(String[] ids) {
    if ((ids != null) && (ids.length > 0)) {
      String policeId = "";
      for (int i = 0; i < ids.length; i++) {
        OTeamUser team = getTeamUser(Integer.parseInt(ids[i]));
        if ("".equals(policeId))
          policeId = team.getPoliceTeam().getId().toString();
        else {
          policeId = policeId + "," + team.getPoliceTeam().getId().toString();
        }
      }
      String teamHql = "delete from OTeamUser where policeTeam.id in(" + policeId + ")";
      this.policeTeamDao.executeHql(teamHql);
      String hql = "delete from OPoliceTeam where id in(" + policeId + ")";
      this.policeTeamDao.executeHql(hql);
    }
  }

  public String getActorName(int policeId)
  {
    String actorName = "";
    String hql = "from OTeamUser o where o.policeTeam.id=" + policeId + " and o.state='" + "coming" + "'";
    List<OTeamUser> list = this.policeTeamDao.findByHql(hql);
    if ((list != null) && (!list.isEmpty())) {
      for (OTeamUser user : list) {
        if (user.getLevels().intValue() == 2)
          actorName = actorName + user.getActorName() + ",";
      }
    }
    else {
      actorName = "已办结";
    }
    return actorName;
  }

  public OTeamUser getTeamUser(int id) {
    return (OTeamUser)this.policeTeamDao.getObject(new OTeamUser(), id);
  }
}

/* Location:           /Volumes/work/oa/WEB-INF/classes/
 * Qualified Name:     com.jcrj.service.PoliceTeamService
 * JD-Core Version:    0.6.0
 */